﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class RevitTools
	{
		public static void HighLightWall(ExternalCommandData cmdData, WallType wallType)
		{
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(cmdData.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(Wall)).OfCategory(BuiltInCategory.OST_Walls);
			List<Element> list = new List<Element>();
			foreach (Element element in filteredElementCollector.ToElements())
			{
				if ((element as Wall).WallType.Name == wallType.Name)
				{
					list.Add(element);
				}
			}
			RevitVersionFuncs.SetSelection(selection, list);
		}

		public static double GetWallParam(Document doc, WallType wallType, string pName)
		{
			Wall wall = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Wall)).OfCategory(BuiltInCategory.OST_Walls);
			foreach (Element element in filteredElementCollector.ToElements())
			{
				Wall wall2 = element as Wall;
				if (wall2.WallType == wallType)
				{
					wall = wall2;
				}
			}
			if (wall != null)
			{
				return wall.GetParameter(pName).AsDouble();
			}
			return 1.0;
		}

		public static double GetWallParam(Wall wall, string pName)
		{
			return wall.GetParameter(pName).AsDouble();
		}

		public static void SetThicknessValue(ExternalCommandData cmdData)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(cmdData.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(Wall)).OfCategory(BuiltInCategory.OST_Walls);
			foreach (Element element in filteredElementCollector.ToElements())
			{
				Wall elem = element as Wall;
				Parameter parameter = elem.GetParameter("导热系数");
				if (!parameter.HasValue)
				{
					parameter.Set(1);
				}
				Parameter parameter2 = elem.GetParameter("修正系数");
				if (!parameter2.HasValue)
				{
					parameter2.Set(1);
				}
			}
		}

		public static void SetThicknessData(ExternalCommandData cmdData)
		{
			string sharedParametersFilename = WinFormTools.hyDataPath() + "ThicknessData.txt";
			cmdData.Application.Application.SharedParametersFilename = sharedParametersFilename;
			DefinitionFile definitionFile = cmdData.Application.Application.OpenSharedParameterFile();
			CategorySet categorySet = cmdData.Application.Application.Create.NewCategorySet();
			Category category = cmdData.Application.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
			categorySet.Insert(category);
			InstanceBinding instanceBinding = cmdData.Application.Application.Create.NewInstanceBinding(categorySet);
			DefinitionGroup definitionGroup = definitionFile.Groups.get_Item("厚度计算");
			if (definitionGroup == null)
			{
				definitionGroup = definitionFile.Groups.Create("厚度计算");
			}
			Definition definition = definitionGroup.Definitions.get_Item("导热系数");
			if (definition == null)
			{
                ParameterType type = (ParameterType)1;
				definition = UnifiedModified.GetDefinition(definitionGroup, "导热系数", type, true, true);
			}
			cmdData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(definition, instanceBinding);
			Definition definition2 = definitionGroup.Definitions.get_Item("修正系数");
			if (definition2 == null)
			{
                ParameterType type2 = (ParameterType)1;
				definition2 = UnifiedModified.GetDefinition(definitionGroup, "修正系数", type2, true, true);
			}
			cmdData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(definition2, instanceBinding);
		}

		public static IList<Reference> SelWall(ExternalCommandData cmdData)
		{
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			Document document = cmdData.Application.ActiveUIDocument.Document;
			IList<Reference> result = null;
			Transaction transaction = new Transaction(document, "选择墙");
			transaction.Start();
			try
			{
				result = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new WallSelectionFilter(), "选择墙：");
				transaction.Commit();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.RollBack();
			}
			catch (Exception ex)
			{
				ex.ToString();
				transaction.RollBack();
			}
			return result;
		}

		public static void ChangeThickness(Document doc, WallType wallType, double dWidth)
		{
			Transaction transaction = new Transaction(doc, "修改保温层厚度");
			try
			{
				transaction.Start();
				if (dWidth < 5.0)
				{
					dWidth = 5.0;
				}
				CompoundStructure compoundStructure = wallType.GetCompoundStructure();
				bool flag = false;
				IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
				foreach (CompoundStructureLayer compoundStructureLayer in layers)
				{
                    if ((int)compoundStructureLayer.Function == 3)
					{
						flag = true;
						compoundStructureLayer.Width = dWidth / 304.8;
					}
				}
				if (!flag)
				{
					CompoundStructureLayer compoundStructureLayer2 = new CompoundStructureLayer();
                    compoundStructureLayer2.Function = (MaterialFunctionAssignment)3;
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
					filteredElementCollector.OfClass(typeof(Material));
					FilteredElementIterator elementIterator = filteredElementCollector.GetElementIterator();
					elementIterator.Reset();
					while (elementIterator.MoveNext())
					{
						Element element = elementIterator.Current;
						Material material = element as Material;
						if (material.Name.Contains("隔热层"))
						{
							compoundStructureLayer2.MaterialId = material.Id;
							break;
						}
					}
					compoundStructureLayer2.Width = dWidth / 304.8;
					layers.Insert(0, compoundStructureLayer2);
				}
				compoundStructure.SetLayers(layers);
				wallType.SetCompoundStructure(compoundStructure);
				transaction.Commit();
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				throw ex;
			}
		}

		public static string GetDrXs(string materName)
		{
			return "1.400";
		}

		public static string GetMaterialClassName(int i)
		{
			string result = "无类别";
			switch (i)
			{
			case 0:
				result = "未定义";
				break;
			case 1:
				result = "钢";
				break;
			case 2:
				result = "具体";
				break;
			case 3:
				result = "木质";
				break;
			case 4:
				result = "其他";
				break;
			case 5:
				result = "混凝土";
				break;
			case 6:
				result = "常规";
				break;
			case 7:
				result = "铝";
				break;
			}
			return result;
		}

		public static FilteredElementCollector comGetCollector(Document doc, Type type, BuiltInCategory builtInCate)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(type).OfCategory(builtInCate);
			return filteredElementCollector;
		}

		public static void GetWallTypeSet(IList<Element> listElem, Document doc, ref List<WallType> basicWallType, ref List<WallType> curtainWallType, ref List<WallType> stackWallType, ref List<WallType> unknowWallType)
		{
			List<WallType> list = new List<WallType>();
			foreach (Element element in listElem)
			{
				Wall wall = element as Wall;
				if (!list.Contains(wall.WallType))
				{
					list.Add(wall.WallType);
				}
			}
			foreach (WallType wallType in list)
			{
				WallType wallType2 = wallType as WallType;
                switch ((int)wallType2.Kind)
				{
				case -1:
					unknowWallType.Add(wallType2);
					break;
				case 0:
					basicWallType.Add(wallType2);
					break;
				case 1:
					curtainWallType.Add(wallType2);
					break;
				case 2:
					stackWallType.Add(wallType2);
					break;
				}
			}
		}

		public static void SetCateVisible(Document doc, BuiltInCategory builtInCate, bool bVisible)
		{
			Transaction transaction = new Transaction(doc, "www");
			transaction.Start();
			Category cate = doc.Settings.Categories.get_Item(builtInCate);
			doc.SetYJKVisibility(cate, bVisible);
			transaction.Commit();
		}

		public static string GetPathByKey(List<string> listPath, string strKey)
		{
			foreach (string text in listPath)
			{
				if (text.Contains(strKey))
				{
					return text;
				}
			}
			return "";
		}

		public static bool SpaceParamIsExist(Space space)
		{
			bool result = true;
			Parameter parameter = space.GetParameter("冬季空调设计温度");
			if (parameter == null || parameter.AsDouble().ToString() == "0")
			{
				result = false;
			}
			return result;
		}

		public static void cmdCreateSpace(ExternalCommandData commandData)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			try
			{
				Transaction transaction = new Transaction(document, "http://www.hongye.com.cn");
				transaction.Start();
				try
				{
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
					filteredElementCollector.OfCategory(BuiltInCategory.OST_Rooms);
					int num = 0;
					RevitTools.LoadAllSpace(commandData);
					foreach (Element element in filteredElementCollector)
					{
						Room room = element as Room;
						RevitTools.CreateSpace(application, room, ref num);
					}
					FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document);
					filteredElementCollector2.OfClass(typeof(Instance)).OfCategory(BuiltInCategory.OST_RvtLinks);
					List<string> linkFilePaths = RevitTools.GetLinkFilePaths(document);
					foreach (Element element2 in filteredElementCollector2)
					{
						Instance instance = element2 as Instance;
						if (instance != null)
						{
							(document.GetElementById(instance.GetTypeId()) as RevitLinkType).GetParameter(BuiltInParameter.WALL_ATTR_ROOM_BOUNDING).Set(1);
							Transform transform = instance.GetTransform();
							string str = element2.Name.Substring(0, element2.Name.IndexOf(".rvt"));
							FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(application.Application.OpenDocumentFile(RevitTools.GetPathByKey(linkFilePaths, str + ".rvt")));
							filteredElementCollector3.OfCategory(BuiltInCategory.OST_Rooms);
							foreach (Element element3 in filteredElementCollector3)
							{
								Room room2 = element3 as Room;
								RevitTools.CreateSpace(application, room2, transform, ref num);
							}
						}
					}
					RevitTools.listAllSpace = null;
					CmdTools.cdSetSpaceValue(commandData);
					if (num > 0)
					{
						MessageBox.Show("空间创建完成。", "盈建科提示");
					}
					else
					{
						MessageBox.Show("没有需要创建的空间。", "盈建科提示");
					}
					transaction.Commit();
				}
				catch (Exception ex)
				{
					WinFormTools.MsgBox(ex.ToString());
					transaction.RollBack();
				}
			}
			catch
			{
			}
		}

		public static void CreateSpace(Document doc, Room room, ref int iCount)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			subTransaction.Start();
			LocationPoint locationPoint = room.Location as LocationPoint;
			UV uv = new UV();
			if (room.Location != null)
			{
				uv = new UV(locationPoint.Point.X, locationPoint.Point.Y);
				try
				{
					if (!RevitTools.SpaceIsExist(room.Name))
					{
						Space space = doc.Create.NewSpace(room.Level, uv);
						space.Name = room.Name.Split(new char[]
						{
							' '
						})[0];
						space.Number = room.Number;
						if (space != null)
						{
							iCount++;
						}
					}
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					WinFormTools.MsgBox(ex.ToString());
				}
				return;
			}
			subTransaction.RollBack();
		}

		public static Space CreateSpace(UIApplication uiApp, Room room)
		{
			Space space = null;
			Document document = uiApp.ActiveUIDocument.Document;
			Selection selection = uiApp.ActiveUIDocument.Selection;
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			LocationPoint locationPoint = room.Location as LocationPoint;
			UV uv = new UV();
			if (room.Location != null)
			{
				uv = new UV(locationPoint.Point.X, locationPoint.Point.Y);
				Space result;
				try
				{
					if (!RevitTools.SpaceIsExist(room.Name))
					{
						space = document.Create.NewSpace(room.Level, uv);
						space.Name = room.Name.Split(new char[]
						{
							' '
						})[0];
						space.Number = room.Number;
						if (space != null)
						{
							RevitVersionFuncs.AddToSelection(selection, space);
						}
					}
					subTransaction.Commit();
					result = space;
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					WinFormTools.MsgBox(ex.ToString());
					result = null;
				}
				return result;
			}
			subTransaction.RollBack();
			return null;
		}

		public static Level GetLevelByName(Document doc, string levelName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels);
			foreach (Element element in filteredElementCollector)
			{
				if (element.Name == levelName)
				{
					return element as Level;
				}
			}
			return null;
		}

		public static void CreateSpace(UIApplication uiApp, Room room, Transform transForm, ref int iCount)
		{
			Document document = uiApp.ActiveUIDocument.Document;
			Selection selection = uiApp.ActiveUIDocument.Selection;
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			if (room.Location == null)
			{
				subTransaction.RollBack();
				return;
			}
			LocationPoint locationPoint = room.Location as LocationPoint;
			UV uv = new UV();
			XYZ xyz = locationPoint.Point + transForm.Origin;
			if (room.Location != null)
			{
				uv = new UV(xyz.X, xyz.Y);
				try
				{
					if (!RevitTools.SpaceIsExist(room.Name))
					{
						Level levelByName = RevitTools.GetLevelByName(document, room.Level.Name);
						if (levelByName != null)
						{
							Space space = document.Create.NewSpace(levelByName, uv);
							space.Name = room.Name.Split(new char[]
							{
								' '
							})[0];
							space.Number = room.Number;
							if (space != null)
							{
								iCount++;
								RevitVersionFuncs.AddToSelection(selection, space);
							}
						}
						else if (!XmlInfo.strLevel.Contains(room.Level.Name))
						{
							XmlInfo.strLevel = XmlInfo.strLevel + room.Level.Name + "\n";
						}
					}
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					WinFormTools.MsgBox(ex.ToString());
				}
				return;
			}
			subTransaction.RollBack();
		}

		public static void CreateSpace(UIApplication uiApp, Room room, ref int iCount)
		{
			Document document = uiApp.ActiveUIDocument.Document;
			Selection selection = uiApp.ActiveUIDocument.Selection;
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			LocationPoint locationPoint = room.Location as LocationPoint;
			UV uv = new UV();
			if (room.Location != null)
			{
				uv = new UV(locationPoint.Point.X, locationPoint.Point.Y);
				try
				{
					if (!RevitTools.SpaceIsExist(room.Name))
					{
						Space space = document.Create.NewSpace(room.Level, uv);
						space.Name = room.Name.Split(new char[]
						{
							' '
						})[0];
						space.Number = room.Number;
						if (space != null)
						{
							iCount++;
							RevitVersionFuncs.AddToSelection(selection, space);
						}
					}
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					subTransaction.RollBack();
					WinFormTools.MsgBox(ex.ToString());
				}
				return;
			}
			subTransaction.RollBack();
		}

		public static List<string> GetLinkFilePaths(Document doc)
		{
			List<string> list = new List<string>();
			foreach (ElementId elementId in ExternalFileUtils.GetAllExternalFileReferences(doc))
			{
				if (doc.GetElementById(elementId).IsExternalFileReference())
				{
					ExternalFileReference externalFileReference = doc.GetElementById(elementId).GetExternalFileReference();
                    if (1 == (int)externalFileReference.ExternalFileReferenceType)
					{
						list.Add(ModelPathUtils.ConvertModelPathToUserVisiblePath(externalFileReference.GetAbsolutePath()));
					}
				}
			}
			return list;
		}

		public static bool SpaceIsExist(string spaceName)
		{
			IEnumerator<Element> enumerator = RevitTools.listAllSpace.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if ((enumerator.Current as Space).Name == spaceName)
					{
						return true;
					}
				}
			}
			return false;
		}

		public static void LoadAllSpace(ExternalCommandData cmdData)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(cmdData.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_MEPSpaces);
			RevitTools.listAllSpace = filteredElementCollector.ToElements();
		}

		public static void SetSpaceValue(Space space, XmlNodeList nodeList)
		{
			if (nodeList != null)
			{
				IEnumerator enumerator = nodeList.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						XmlNode xmlNode = (XmlNode)obj;
						Parameter parameter = space.GetParameter(xmlNode.Attributes["name"].Value);
						if (parameter != null)
						{
							if (xmlNode.Attributes["name"].Value.Contains("温度") || xmlNode.Attributes["name"].Value.Contains("湿度"))
							{
								double num;
								if (double.TryParse(xmlNode.Attributes["value"].Value, out num))
								{
									space.GetParameter(xmlNode.Attributes["name"].Value).Set(273.15 + Convert.ToDouble(xmlNode.Attributes["value"].Value));
								}
							}
                            else if ((int)parameter.StorageType == 2)
							{
								double num;
								if (double.TryParse(xmlNode.Attributes["value"].Value, out num))
								{
									space.GetParameter(xmlNode.Attributes["name"].Value).Set(Convert.ToDouble(xmlNode.Attributes["value"].Value));
								}
							}
                            else if ((int)parameter.StorageType == 1)
							{
								int num2;
								if (int.TryParse(xmlNode.Attributes["value"].Value, out num2))
								{
									space.GetParameter(xmlNode.Attributes["name"].Value).Set(Convert.ToInt32(xmlNode.Attributes["value"].Value));
								}
							}
							else
							{
								space.GetParameter(xmlNode.Attributes["name"].Value).Set(xmlNode.Attributes["value"].Value);
							}
							RevitTools.SetSpaceValue(space, xmlNode);
						}
					}
					return;
				}
			}
			WinFormTools.MsgBox("没有空间附加数据");
		}

		public static void SetSingleSpaceValue(Space space)
		{
			XmlNodeList spaceTypeValue = XmlInfo.GetSpaceTypeValue("默认");
			RevitTools.SetSpaceValue(space, spaceTypeValue);
		}

		public static void SetSpaceValue(Space space, XmlNode node)
		{
			try
			{
				double num = 0.0;
				double num2 = 0.0;
				if (double.TryParse(node.Attributes["value"].Value, out num))
				{
					num2 = double.Parse(node.Attributes["value"].Value);
				}
				num2 += 273.15;
				string value = node.Attributes["name"].Value;
				uint num3 = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(value);
				if (num3 <= 1187916273u)
				{
					if (num3 <= 777034711u)
					{
						if (num3 != 398293269u)
						{
							if (num3 == 777034711u)
							{
								if (!(value == "人员密度"))
								{
								}
							}
						}
						else if (!(value == "总冷指标"))
						{
						}
					}
					else if (num3 != 860791076u)
					{
						if (num3 != 972359194u)
						{
							if (num3 == 1187916273u)
							{
								if (value == "冬季空调设计温度")
								{
									space.Zone.HeatingSetPoint = num2;
								}
							}
						}
						else if (value == "冬季采暖设计相对湿度")
						{
							space.Zone.HumidificationSetPoint = (num2 - 273.15) / 100.0;
						}
					}
					else if (value == "人员总数")
					{
						space.NumberofPeople = num2;
					}
				}
				else if (num3 <= 1419873716u)
				{
					if (num3 != 1222474262u)
					{
						if (num3 != 1304331011u)
						{
							if (num3 == 1419873716u)
							{
								if (value == "新风量")
								{
									space.DesignSupplyAirflow = num2;
								}
							}
						}
						else if (!(value == "总热指标"))
						{
						}
					}
					else if (value == "夏季空调设计温度")
					{
						space.Zone.CoolingSetPoint = num2;
					}
				}
				else if (num3 != 2369061932u)
				{
					if (num3 != 2457258827u)
					{
						if (num3 == 3904809635u)
						{
							if (value == "冬季采暖设计温度")
							{
								space.Zone.HeatingSetPoint = num2;
							}
						}
					}
					else if (value == "夏季空调设计相对湿度")
					{
						space.Zone.DehumidificationSetPoint = (num2 - 273.15) / 100.0;
					}
				}
				else if (value == "冬季空调设计相对湿度")
				{
					space.Zone.HumidificationSetPoint = (num2 - 273.15) / 100.0;
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		public static void SetSpaceData(ExternalCommandData cmdData, XmlNodeList nodeList)
		{
			cmdData.Application.Application.SharedParametersFilename = Product.InstallLocation + "\\Data\\SpaceData.txt";
			DefinitionFile definitionFile = cmdData.Application.Application.OpenSharedParameterFile();
			CategorySet categorySet = cmdData.Application.Application.Create.NewCategorySet();
			Category category = cmdData.Application.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_MEPSpaces);
			categorySet.Insert(category);
			InstanceBinding instanceBinding = cmdData.Application.Application.Create.NewInstanceBinding(categorySet);
			DefinitionGroup definitionGroup = definitionFile.Groups.get_Item("盈建科空间附加数据");
			if (definitionGroup == null)
			{
				definitionGroup = definitionFile.Groups.Create("盈建科空间附加数据");
			}
			foreach (object obj in nodeList)
			{
				XmlNode xmlNode = (XmlNode)obj;
				Definition definition = definitionGroup.Definitions.get_Item(xmlNode.Attributes["name"].Value);
				if (definition == null)
				{
                    ParameterType type = (ParameterType)1;
					UnifiedModified.GetDefinition(definitionGroup, xmlNode.Attributes["name"].Value, type, true, true);
				}
				cmdData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(definition, instanceBinding);
			}
		}

		public static void SetCateElem(ElementSet elementSet, UIDocument uiDoc, Type type)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiDoc.Document);
			filteredElementCollector.OfClass(type);
			foreach (Element element in filteredElementCollector)
			{
				if (element.CanBeHidden(uiDoc.ActiveView))
				{
					elementSet.Insert(element);
				}
			}
		}

		public static void SetCateElem(ElementSet elementSet, UIDocument uiDoc, Type type, BuiltInCategory builtInCate)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiDoc.Document);
			if (type != null)
			{
				filteredElementCollector.OfClass(type).OfCategory(builtInCate);
			}
			else
			{
				filteredElementCollector.OfCategory(builtInCate);
			}
			foreach (Element element in filteredElementCollector)
			{
				if (element.CanBeHidden(uiDoc.ActiveView))
				{
					elementSet.Insert(element);
				}
			}
		}

		public static ElementSet hyGetAllElementThread(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.WherePasses(new LogicalOrFilter(new ElementIsElementTypeFilter(false), new ElementIsElementTypeFilter(true)));
			foreach (Element element in filteredElementCollector)
			{
				elementSet.Insert(element);
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet hyGetAllElement(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			RevitTools.SetCateElem(elementSet, activeUIDocument, typeof(Instance), BuiltInCategory.OST_RvtLinks);
			RevitTools.SetCateElem(elementSet, activeUIDocument, typeof(FamilyInstance));
			RevitTools.SetCateElem(elementSet, activeUIDocument, typeof(Duct), BuiltInCategory.OST_DuctCurves);
			RevitTools.SetCateElem(elementSet, activeUIDocument, typeof(Pipe), BuiltInCategory.OST_PipeCurves);
			RevitTools.SetCateElem(elementSet, activeUIDocument, typeof(Wall), BuiltInCategory.OST_Walls);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Floors);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Columns);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_StructuralColumns);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Ceilings);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_GenericModel);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_StairsRailing);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Stairs);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_FlexDuctCurves);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_CableTray);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_MEPSpaceSeparationLines);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Conduit);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Lines);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, UnifiedModified.GetAreaAreaSeparationLines());
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Wire);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_Rooms);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_RoomTags);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_MEPSpaces);
			RevitTools.SetCateElem(elementSet, activeUIDocument, null, BuiltInCategory.OST_MEPSpaceTags);
			subTransaction.Commit();
			return elementSet;
		}

		public static string hyGetCurrentSel(ExternalCommandData revit)
		{
			RibbonPanel ribbonPanel = null;
			foreach (RibbonPanel ribbonPanel2 in revit.Application.GetRibbonPanels("盈建科工具"))
			{
				if (ribbonPanel2.Name == "选择集")
				{
					ribbonPanel = ribbonPanel2;
					break;
				}
			}
			ComboBox comboBox = null;
			foreach (RibbonItem ribbonItem in ribbonPanel.GetItems())
			{
				if (ribbonItem.Name == "selList")
				{
					comboBox = (ribbonItem as ComboBox);
				}
			}
			if (comboBox != null)
			{
				return comboBox.Current.ItemText;
			}
			return "";
		}

		public static ElementSet hyGetDuctAccessory(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_DuctAccessory);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetDuctFitting(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_DuctFitting);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyFromFilter(UIDocument uiDoc, ElementSet elementSet, string selName)
		{
			ElementSet elementSet2 = new ElementSet();
			foreach (object obj in elementSet)
			{
				Element element = (Element)obj;
				bool flag = true;
				bool flag2 = true;
				bool flag3 = true;
				bool flag4 = true;
				XmlNode xmlNode = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Text='" + selName + "']/Filters/Filter[@Name=\"过滤器1\"]");
				XmlNode xmlNode2 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Text='" + selName + "']/Filters/Filter[@Name=\"过滤器2\"]");
				XmlNode xmlNode3 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Text='" + selName + "']/Filters/Filter[@Name=\"过滤器3\"]");
				XmlNode xmlNode4 = XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Text='" + selName + "']/Filters/Filter[@Name=\"过滤器4\"]");
				foreach (object obj2 in element.Parameters)
				{
					Parameter parameter = (Parameter)obj2;
					string paramVal = RevitTools.GetParamVal(uiDoc.Document, parameter);
					if (parameter.Definition.Name == xmlNode.Attributes["Attr"].Value && xmlNode.Attributes["Valid"].Value == "true")
					{
						flag = RevitTools.FormulaCheck(paramVal, xmlNode.Attributes["Value"].Value, xmlNode.Attributes["Formula"].Value, parameter);
					}
					if (parameter.Definition.Name == xmlNode2.Attributes["Attr"].Value && xmlNode2.Attributes["Valid"].Value == "true")
					{
						flag2 = RevitTools.FormulaCheck(paramVal, xmlNode2.Attributes["Value"].Value, xmlNode2.Attributes["Formula"].Value, parameter);
					}
					if (parameter.Definition.Name == xmlNode3.Attributes["Attr"].Value && xmlNode3.Attributes["Valid"].Value == "true")
					{
						flag3 = RevitTools.FormulaCheck(paramVal, xmlNode3.Attributes["Value"].Value, xmlNode3.Attributes["Formula"].Value, parameter);
					}
					if (parameter.Definition.Name == xmlNode4.Attributes["Attr"].Value && xmlNode4.Attributes["Valid"].Value == "true")
					{
						flag4 = RevitTools.FormulaCheck(paramVal, xmlNode4.Attributes["Value"].Value, xmlNode4.Attributes["Formula"].Value, parameter);
					}
				}
				if (flag && flag2 && flag3 && flag4)
				{
					elementSet2.Insert(element);
				}
			}
			return elementSet2;
		}

		public static ElementSet hyGetLightingFixtures(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_LightingFixtures);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetLightingDevices(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_LightingDevices);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetElectricalFixtures(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_ElectricalFixtures);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetDongli(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_ElectricalEquipment);
			ElementSet elementSet = new ElementSet();
			foreach (object obj in cateElem)
			{
				Element element = (Element)obj;
				if (element.GetParameter("所属配电箱") != null)
				{
					elementSet.Insert(element);
				}
			}
			return RevitTools.hyFromFilter(uiDoc, elementSet, selName);
		}

		public static ElementSet hyGetXiangGui(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_ElectricalEquipment);
			ElementSet elementSet = new ElementSet();
			foreach (object obj in cateElem)
			{
				Element element = (Element)obj;
				if (element.GetParameter("所属配电箱") == null)
				{
					elementSet.Insert(element);
				}
			}
			return RevitTools.hyFromFilter(uiDoc, elementSet, selName);
		}

		public static ElementSet hyGetDuct(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(Duct), BuiltInCategory.OST_DuctCurves);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetWall(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(Wall), BuiltInCategory.OST_Walls);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetDoor(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_Doors);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetWindows(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_Windows);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetFloors(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(Floor), BuiltInCategory.OST_Floors);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetCeilings(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(CeilingAndFloor), BuiltInCategory.OST_Ceilings);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetCableTray(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(CableTray), BuiltInCategory.OST_CableTray);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetWire(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(Wire), BuiltInCategory.OST_Wire);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetConduit(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(Conduit), BuiltInCategory.OST_Conduit);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetCableTrayFitting(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_CableTrayFitting);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetColumns(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_Columns);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetConduitFitting(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_ConduitFitting);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetPipeAccessory(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_PipeAccessory);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetMech(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_MechanicalEquipment);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetFengJi(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_MechanicalEquipment);
			ElementSet elementSet = new ElementSet();
			foreach (object obj in cateElem)
			{
				Element element = (Element)obj;
				if (RevitTools.IsFengJi(element as FamilyInstance))
				{
					elementSet.Insert(element);
				}
			}
			return RevitTools.hyFromFilter(uiDoc, elementSet, selName);
		}

		public static bool IsFengJi(FamilyInstance fi)
		{
			bool flag = false;
			bool flag2 = false;
			foreach (object obj in fi.MEPModel.ConnectorManager.Connectors)
			{
				Connector connector = (Connector)obj;
				if (connector.Domain == (Autodesk.Revit.DB.Domain)1)
				{
					flag = true;
				}
				else if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
				{
					flag2 = true;
				}
			}
			return flag && !flag2;
		}

		public static bool IsPanGuan(FamilyInstance fi)
		{
			bool flag = false;
			bool flag2 = false;
			foreach (object obj in fi.MEPModel.ConnectorManager.Connectors)
			{
				Connector connector = (Connector)obj;
				if (connector.Domain == (Autodesk.Revit.DB.Domain)1)
				{
					flag = true;
				}
				else if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
				{
					flag2 = true;
				}
			}
			return flag && flag2;
		}

		public static ElementSet hyGetPanGuan(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_MechanicalEquipment);
			ElementSet elementSet = new ElementSet();
			foreach (object obj in cateElem)
			{
				Element element = (Element)obj;
				if (RevitTools.IsPanGuan(element as FamilyInstance))
				{
					elementSet.Insert(element);
				}
			}
			return RevitTools.hyFromFilter(uiDoc, elementSet, selName);
		}

		public static ElementSet hyGetPipeFitting(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_PipeFitting);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetPipe(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(Pipe), BuiltInCategory.OST_PipeCurves);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetDuctTerminal(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(FamilyInstance), BuiltInCategory.OST_DuctTerminal);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet hyGetDuctTerminalTag(UIDocument uiDoc, string selName)
		{
			ElementSet cateElem = RevitTools.GetCateElem(uiDoc, typeof(IndependentTag), BuiltInCategory.OST_DuctTerminalTags);
			return RevitTools.hyFromFilter(uiDoc, cateElem, selName);
		}

		public static ElementSet GetCateElem(UIDocument uiDoc, BuiltInCategory builtInCate)
		{
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiDoc.Document);
			filteredElementCollector.OfCategory(builtInCate);
			foreach (Element element in filteredElementCollector)
			{
				elementSet.Insert(element);
			}
			return elementSet;
		}

		public static ElementSet GetCateElem(UIDocument uiDoc, Type type, BuiltInCategory builtInCate)
		{
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(uiDoc.Document);
			filteredElementCollector.OfClass(type).OfCategory(builtInCate);
			foreach (Element element in filteredElementCollector)
			{
				elementSet.Insert(element);
			}
			return elementSet;
		}

		public static string GetParamVal(Document doc, Parameter p)
		{
			string result = "";
            switch ((int)p.StorageType)
			{
			case 1:
				result = p.AsInteger().ToString();
				break;
			case 2:
				result = p.AsDouble().ToString();
				break;
			case 3:
				result = p.AsString();
				break;
			case 4:
				if (doc.GetElementById(p.AsElementId()) != null)
				{
					result = doc.GetElementById(p.AsElementId()).Name;
				}
				break;
			}
			return result;
		}

		public static bool FormulaCheck(string pVal, string attrVal, string formula, Parameter p)
		{
			bool result = false;
			double num = 0.0;
			double num2 = 1.0;
            if ((int)p.StorageType == 2 || (int)p.StorageType == 1)
			{
				num = double.Parse(pVal);
				num2 = double.Parse(attrVal);
			}
            num = UnitConvert.CovertFromAPI((DisplayUnitType)2, num);
			if (!(formula == "等于"))
			{
				if (!(formula == "不等于"))
				{
					if (!(formula == "大于"))
					{
						if (formula == "小于")
						{
							if (num < num2)
							{
								result = true;
							}
						}
					}
					else if (num > num2)
					{
						result = true;
					}
				}
				else if (pVal != attrVal && num != num2)
				{
					result = true;
				}
			}
			else if (pVal == attrVal)
			{
				result = true;
			}
			else if (num == num2)
			{
				result = true;
			}
			return result;
		}

		public static FilteredElementCollector GetDuctTerminal(ExternalCommandData cmdData)
		{
			return RevitTools.GetCategoryElement(cmdData, typeof(FamilyInstance), BuiltInCategory.OST_DuctTerminal);
		}

		public static FilteredElementCollector GetDuctFitting(ExternalCommandData cmdData)
		{
			return RevitTools.GetCategoryElement(cmdData, typeof(FamilyInstance), BuiltInCategory.OST_DuctFitting);
		}

		public static FilteredElementCollector GetPipe(ExternalCommandData cmdData)
		{
			return RevitTools.GetCategoryElement(cmdData, typeof(Pipe), BuiltInCategory.OST_PipeCurves);
		}

		public static FilteredElementCollector GetDuct(ExternalCommandData cmdData)
		{
			return RevitTools.GetCategoryElement(cmdData, typeof(Duct), BuiltInCategory.OST_DuctCurves);
		}

		public static double mmToFeet(double val)
		{
			return val / 304.8;
		}

		public static double feetToMm(double val)
		{
			return val * 304.8;
		}

		public static FilteredElementCollector GetCategoryElement(ExternalCommandData cmdData, Type objType, BuiltInCategory cate)
		{
			Document document = cmdData.Application.ActiveUIDocument.Document;
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfClass(objType).OfCategory(cate);
			return filteredElementCollector;
		}

		public static Result LockElements(ExternalCommandData commandData, List<ElementId> listID, bool isLock)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Transaction transaction = new Transaction(activeUIDocument.Document, "LockUnLock");
			transaction.Start();
			View activeView = activeUIDocument.ActiveView;
			new ElementSet().Clear();
			foreach (ElementId elementId in listID)
			{
				activeUIDocument.Document.GetElementById(elementId).Pinned = isLock;
			}
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result ShowHideElement(ExternalCommandData commandData, List<ElementId> listID, bool isShow)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Transaction transaction = new Transaction(activeUIDocument.Document, "ShowHide");
			transaction.Start();
			View activeView = activeUIDocument.ActiveView;
			ElementSet elementSet = new ElementSet();
			elementSet.Clear();
			foreach (ElementId elementId in listID)
			{
				Element elementById = activeUIDocument.Document.GetElementById(elementId);
				if (elementById.CanBeHidden(activeView))
				{
					elementSet.Insert(elementById);
				}
			}
			if (isShow)
			{
				activeView.UnHideYJKElements(elementSet);
			}
			else
			{
				activeView.HideYJKElements(elementSet);
			}
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result SelSelectSet(ExternalCommandData commandData, List<ElementId> listID)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Transaction transaction = new Transaction(activeUIDocument.Document, "SelSelectSet");
			transaction.Start();
			Selection selection = activeUIDocument.Selection;
			bool flag = true;
			if (listID.Count > 1000)
			{
                flag = ((int)TaskDialog.Show("盈建科提示", "要操作的元素数量：" + listID.Count + "个，可能会导致死机，是否继续？", (TaskDialogCommonButtons)5) == 1);
			}
			if (flag)
			{
				List<Element> list = new List<Element>();
				foreach (ElementId elementId in listID)
				{
					Element elementById = activeUIDocument.Document.GetElementById(elementId);
					list.Add(elementById);
				}
				RevitVersionFuncs.SetSelection(selection, list);
			}
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static ElementSet GetAllElement(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);
			foreach (Element element in filteredElementCollector)
			{
				if (element.CanBeHidden(activeView))
				{
					elementSet.Insert(element);
				}
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector2.OfClass(typeof(Wall)).OfCategory(BuiltInCategory.OST_Walls);
			foreach (Element element2 in filteredElementCollector2)
			{
				if (element2.CanBeHidden(activeView))
				{
					elementSet.Insert(element2);
				}
			}
			FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector3.OfClass(typeof(FamilyInstance));
			foreach (Element element3 in filteredElementCollector3)
			{
				if (element3.CanBeHidden(activeView))
				{
					elementSet.Insert(element3);
				}
			}
			FilteredElementCollector filteredElementCollector4 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector4.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors);
			foreach (Element element4 in filteredElementCollector4)
			{
				if (element4.CanBeHidden(activeView))
				{
					elementSet.Insert(element4);
				}
			}
			FilteredElementCollector filteredElementCollector5 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector5.OfCategory(BuiltInCategory.OST_Floors);
			foreach (Element element5 in filteredElementCollector5)
			{
				if (element5.CanBeHidden(activeView))
				{
					elementSet.Insert(element5);
				}
			}
			FilteredElementCollector filteredElementCollector6 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector6.OfCategory(BuiltInCategory.OST_Columns);
			foreach (Element element6 in filteredElementCollector6)
			{
				if (element6.CanBeHidden(activeView))
				{
					elementSet.Insert(element6);
				}
			}
			FilteredElementCollector filteredElementCollector7 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector7.OfCategory(BuiltInCategory.OST_StructuralColumns);
			foreach (Element element7 in filteredElementCollector7)
			{
				if (element7.CanBeHidden(activeView))
				{
					elementSet.Insert(element7);
				}
			}
			FilteredElementCollector filteredElementCollector8 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector8.OfCategory(BuiltInCategory.OST_Ceilings);
			foreach (Element element8 in filteredElementCollector8)
			{
				if (element8.CanBeHidden(activeView))
				{
					elementSet.Insert(element8);
				}
			}
			FilteredElementCollector filteredElementCollector9 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector9.OfCategory(BuiltInCategory.OST_GenericModel);
			foreach (Element element9 in filteredElementCollector9)
			{
				if (element9.CanBeHidden(activeView))
				{
					elementSet.Insert(element9);
				}
			}
			FilteredElementCollector filteredElementCollector10 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector10.OfCategory(BuiltInCategory.OST_StairsRailing);
			foreach (Element element10 in filteredElementCollector10)
			{
				if (element10.CanBeHidden(activeView))
				{
					elementSet.Insert(element10);
				}
			}
			FilteredElementCollector filteredElementCollector11 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector11.OfCategory(BuiltInCategory.OST_Stairs);
			foreach (Element element11 in filteredElementCollector11)
			{
				if (element11.CanBeHidden(activeView))
				{
					elementSet.Insert(element11);
				}
			}
			FilteredElementCollector filteredElementCollector12 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector12.OfCategory(BuiltInCategory.OST_FlexDuctCurves);
			foreach (Element element12 in filteredElementCollector12)
			{
				if (element12.CanBeHidden(activeView))
				{
					elementSet.Insert(element12);
				}
			}
			FilteredElementCollector filteredElementCollector13 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector13.OfCategory(BuiltInCategory.OST_CableTray);
			foreach (Element element13 in filteredElementCollector13)
			{
				if (element13.CanBeHidden(activeView))
				{
					elementSet.Insert(element13);
				}
			}
			FilteredElementCollector filteredElementCollector14 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector14.OfCategory(BuiltInCategory.OST_MEPSpaceSeparationLines);
			foreach (Element element14 in filteredElementCollector14)
			{
				if (element14.CanBeHidden(activeView))
				{
					elementSet.Insert(element14);
				}
			}
			FilteredElementCollector filteredElementCollector15 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector15.OfCategory(BuiltInCategory.OST_Conduit);
			foreach (Element element15 in filteredElementCollector15)
			{
				if (element15.CanBeHidden(activeView))
				{
					elementSet.Insert(element15);
				}
			}
			FilteredElementCollector filteredElementCollector16 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector16.OfCategory(BuiltInCategory.OST_Lines);
			foreach (Element element16 in filteredElementCollector16)
			{
				if (element16.CanBeHidden(activeView))
				{
					elementSet.Insert(element16);
				}
			}
			FilteredElementCollector filteredElementCollector17 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector17.OfCategory(UnifiedModified.GetAreaAreaSeparationLines());
			foreach (Element element17 in filteredElementCollector17)
			{
				if (element17.CanBeHidden(activeView))
				{
					elementSet.Insert(element17);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ICollection<Element> GetAllLevel(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			ICollection<Element> result;
			try
			{
				SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
				subTransaction.Start();
				ICollection<Element> collection = new FilteredElementCollector(activeUIDocument.Document).OfClass(typeof(Level)).ToElements();
				subTransaction.Commit();
				result = collection;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = null;
			}
			return result;
		}

		public static ICollection<Element> GetAllLevelVal(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			ICollection<Element> result;
			try
			{
				result = new FilteredElementCollector(activeUIDocument.Document).OfClass(typeof(Level)).ToElements();
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = null;
			}
			return result;
		}

		public static Result SelectAll(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			try
			{
				Transaction transaction = new Transaction(activeUIDocument.Document, "SelectAll");
				transaction.Start();
				Selection selection = activeUIDocument.Selection;
				ElementSet allElement = RevitTools.GetAllElement(commandData);
				RevitVersionFuncs.ClearSelection(selection);
				List<Element> list = new List<Element>();
				foreach (object obj in allElement)
				{
					Element item = (Element)obj;
					list.Add(item);
				}
				RevitVersionFuncs.SetSelection(selection, list);
				transaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static void SetSelectStr(UIDocument uiDoc, string str)
		{
			try
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("720080CB-DA99-40DC-9415-E53F280AA1F1"));
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetVendorId("ADSK");
				schemaBuilder.AddSimpleField("盈建科选择集", typeof(string)).SetDocumentation("盈建科选择集模块");
				schemaBuilder.SetSchemaName("盈建科选择集");
				Schema schema = schemaBuilder.Finish();
				Entity entity = new Entity(schema);
				Field field = schema.GetField("盈建科选择集");
				entity.Set<string>(field, str, 0);
				uiDoc.Document.ProjectInformation.SetEntity(entity);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		public static string GetSelectStr(UIDocument uiDoc)
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("720080CB-DA99-40DC-9415-E53F280AA1F1"));
			schemaBuilder.SetReadAccessLevel((AccessLevel)1);
			schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
			schemaBuilder.SetVendorId("ADSK");
			schemaBuilder.AddSimpleField("盈建科选择集", typeof(string)).SetDocumentation("盈建科选择集模块");
			schemaBuilder.SetSchemaName("盈建科选择集");
			Schema schema = schemaBuilder.Finish();
			new Entity(schema);
			schema.GetField("盈建科选择集");
			Entity entity = uiDoc.Document.ProjectInformation.GetEntity(schema);
			string result;
			try
			{
				result = entity.Get<string>(schema.GetField("盈建科选择集"), 0);
			}
			catch
			{
				result = "";
			}
			return result;
		}

		public static double getPipeLength(UIApplication uiApp, Pipe pipe)
		{
			LocationCurve locationCurve = pipe.Location as LocationCurve;
			XYZ hyendPoint = locationCurve.Curve.GetEndPoint(0);
			XYZ hyendPoint2 = locationCurve.Curve.GetEndPoint(1);
			return RevitTools.feetToMm(uiApp.ActiveUIDocument.Document.CreatYJKLine(hyendPoint, hyendPoint2, true).Length);
		}

		public static string CountGetPipeMater(Document doc, Pipe pipe)
		{
			string result = "";
			PipeType pipeType = pipe.PipeType;
			if (pipeType != null)
			{
				foreach (object obj in pipeType.Parameters)
				{
					Parameter parameter = (Parameter)obj;
					if (parameter.Definition.Name == "材质")
					{
						result = (doc.GetElementById(parameter.AsElementId()) as Material).Name;
					}
				}
			}
			return result;
		}

		public static string CountGetPipeSpec(Document doc, Pipe pipe)
		{
			string result = "";
			decimal d = 0m;
			foreach (object obj in pipe.Parameters)
			{
				Parameter parameter = (Parameter)obj;
				if (parameter.Definition.Name == "直径")
				{
					if (decimal.TryParse(parameter.AsValueString(), out d))
					{
						decimal num = Math.Ceiling(d);
						if (pipe.PipeType.Name == "标准")
						{
							result = "DN" + num;
						}
						else if (pipe.PipeType.Name == "PVC-U")
						{
							result = "DE" + num;
						}
					}
					else
					{
						result = parameter.AsValueString();
					}
				}
			}
			return result;
		}

		public static List<string> CountGetPipeMater(ExternalCommandData commandData)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			List<string> list = new List<string>();
			Transaction transaction = new Transaction(document, "http://www.hongye.com.cn");
			transaction.Start();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfClass(typeof(PipeType)).OfCategory(BuiltInCategory.OST_PipeCurves);
			foreach (Element element in filteredElementCollector.ToElements())
			{
				PipeType pipeType = element as PipeType;
				if (pipeType != null)
				{
					foreach (object obj in pipeType.Parameters)
					{
						Parameter parameter = (Parameter)obj;
						if (parameter.Definition.Name == "材质")
						{
							Material material = document.GetElementById(parameter.AsElementId()) as Material;
							list.Add(material.Name);
						}
					}
				}
			}
			transaction.Commit();
			return list;
		}

		public static List<string> CountGetPipeSpec(ExternalCommandData commandData)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			List<string> list = new List<string>();
			Transaction transaction = new Transaction(document, "http://www.hongye.com.cn");
			transaction.Start();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfClass(typeof(Pipe)).OfCategory(BuiltInCategory.OST_PipeCurves);
			decimal d = 0m;
			foreach (Element element in filteredElementCollector.ToElements())
			{
				Pipe pipe = element as Pipe;
				if (pipe != null)
				{
					foreach (object obj in pipe.Parameters)
					{
						Parameter parameter = (Parameter)obj;
						if (parameter.Definition.Name == "直径")
						{
							if (decimal.TryParse(parameter.AsValueString(), out d))
							{
								decimal num = Math.Ceiling(d);
								if (pipe.PipeType.Name == "标准")
								{
									list.Add("DN" + num);
								}
								else if (pipe.PipeType.Name == "PVC-U")
								{
									list.Add("DE" + num);
								}
							}
							else
							{
								list.Add(parameter.AsValueString());
							}
						}
					}
				}
			}
			transaction.Commit();
			return list;
		}

		public static ElementSet FilterDuct(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);
			foreach (Element element in filteredElementCollector)
			{
				Duct duct = element as Duct;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == duct.ReferenceLevel.Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet FilterPipe(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(Pipe)).OfCategory(BuiltInCategory.OST_PipeCurves);
			foreach (Element element in filteredElementCollector)
			{
				Pipe pipe = element as Pipe;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == pipe.ReferenceLevel.Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet FilterWall(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(Wall)).OfCategory(BuiltInCategory.OST_Walls);
			foreach (Element element in filteredElementCollector)
			{
				Wall elem = element as Wall;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == elem.GetYJKLevel().Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet FilterDoor(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors);
			foreach (Element element in filteredElementCollector)
			{
				FamilyInstance elem = element as FamilyInstance;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == elem.GetYJKLevel().Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet FilterWindow(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);
			foreach (Element element in filteredElementCollector)
			{
				FamilyInstance elem = element as FamilyInstance;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == elem.GetYJKLevel().Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet FilterFitting(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_DuctFitting);
			foreach (Element element in filteredElementCollector)
			{
				FamilyInstance elem = element as FamilyInstance;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == elem.GetYJKLevel().Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static ElementSet FilterDuctTerminal(ExternalCommandData commandData, List<string> listLevel)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			View activeView = activeUIDocument.Document.ActiveView;
			SubTransaction subTransaction = new SubTransaction(activeUIDocument.Document);
			subTransaction.Start();
			ElementSet elementSet = new ElementSet();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_DuctTerminal);
			foreach (Element element in filteredElementCollector)
			{
				FamilyInstance elem = element as FamilyInstance;
				if (listLevel.Count > 0)
				{
					bool flag = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == elem.GetYJKLevel().Name)
							{
								flag = true;
							}
						}
					}
					if (flag)
					{
						elementSet.Insert(element);
					}
				}
				else
				{
					elementSet.Insert(element);
				}
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(activeUIDocument.Document);
			filteredElementCollector2.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_DuctTerminalTags);
			foreach (Element element2 in filteredElementCollector2)
			{
				FamilyInstance elem2 = element2 as FamilyInstance;
				if (listLevel.Count > 0)
				{
					bool flag2 = false;
					using (List<string>.Enumerator enumerator2 = listLevel.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if (enumerator2.Current == elem2.GetYJKLevel().Name)
							{
								flag2 = true;
							}
						}
					}
					if (flag2)
					{
						elementSet.Insert(element2);
					}
				}
				else
				{
					elementSet.Insert(element2);
				}
			}
			subTransaction.Commit();
			return elementSet;
		}

		public static IList<Element> listAllSpace;
	}
}
